package com.browsesoft.noshow;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.browsesoft.Entity;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;

public class BasicNoRowFilter implements NoShow {

	public List filterShow(Element configElem, Collection datas, HTMLBasicComponent comp) {
		List result = new LinkedList();
		result.addAll(datas);
		NodeList list = configElem.getElementsByTagName("condition");
		for (int i = 0; i < list.getLength(); i++) {
			Element e = (Element) list.item(i);
			String condName = e.getAttribute("name");
			String condValue = e.getAttribute("value");
			result = filterDatas(condName, condValue, result);
		}
        return result;
	}

	private List filterDatas(String condName, String condValue, Collection datas) {
		List result = new LinkedList();
		result.addAll(datas);
		Iterator iter = datas.iterator();
		while (iter.hasNext()) {
			Entity entity = (Entity) iter.next();
			if (entity.getAttributes().get(condName) == null) {
				continue;
			}
			if (entity.getAttributes().get(condName).equals(condValue)) {
				result.remove(entity);
			}
		}
		return result;
	}
}
